//
//  MainController.m
//  WYNews
//
//  Created by xianhui.xiong on 17/1/5.
//  Copyright © 2017年 熊先辉. All rights reserved.
//

#import "MainController.h"

@interface MainController ()

@end

@implementation MainController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.view.backgroundColor = [UIColor darkGrayColor];
    
    //添加子控制器
    [self addChildViewControllers];
    
    //设置tabbar的tintColor
    self.tabBar.tintColor = [UIColor cz_colorWithHex:0xdf0000];
    
}

//添加所有子控制器
- (void) addChildViewControllers {
    
    //所有子控制器的参数
    NSArray *parameters = @[
                            @{@"clsName": @"NewsController", @"title": @"新闻", @"icon": @"news"},
                            @{@"clsName": @"ReadController", @"title": @"阅读", @"icon": @"reader"},
                            @{@"clsName": @"VideoController", @"title": @"视频", @"icon": @"media"},
                            @{@"clsName": @"QuessionController", @"title": @"话题", @"icon": @"bar"},
                            @{@"clsName": @"MeinController", @"title": @"我", @"icon": @"me"}
                            ];
    
    NSMutableArray *array = [NSMutableArray array];
    
    for (NSDictionary *dic in parameters) {
        
        UIViewController *controller = [self addControllerWithDic:dic];
        [array addObject:controller];
    }
    
    self.viewControllers = array;
}


//创建一个子控制器
- (UIViewController *) addControllerWithDic:(NSDictionary *)dic {
    
    //用类名来创建一个控制器
    NSString *clsName = dic[@"clsName"];
    Class cls = NSClassFromString(clsName);
    UIViewController *controller = [cls new];
    
    //设title
    controller.title = dic[@"title"];
    
    //设tabbarItem的图标
    NSString *normalImageName = [NSString stringWithFormat:@"tabbar_icon_%@_normal", dic[@"icon"]];
    NSString *hlImageName = [NSString stringWithFormat:@"tabbar_icon_%@_highlight", dic[@"icon"]];
    
    [controller.tabBarItem setImage:[UIImage imageNamed:normalImageName]];
    [controller.tabBarItem setSelectedImage:[[UIImage imageNamed:hlImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    return [[UINavigationController alloc] initWithRootViewController:controller];
}


@end
